package v3.common.util;

import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import v3.common.exception.BadRequestException;

import java.util.ArrayList;
import java.util.List;

public class FuncUtil {
    public static <T> void validForm(T form, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            List<FieldError> fieldErrorList = bindingResult.getFieldErrors();
            List<String> errors = new ArrayList<>();
            for (FieldError e : fieldErrorList) {
                errors.add(String.format("%s:%s", e.getField(), e.getDefaultMessage()));
            }
            String error = String.join(";", errors);
            throw new BadRequestException(error);
        }
    }
}
